﻿// <copyright file="NeighbourSquares.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the class NeighbourSquares</summary>

namespace Sqwarea.GameLogic.Connexity
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using Sqwarea.GameLogic.Extensions;
    using Sqwarea.Persistence;

    /// <summary>
    /// An enumerator provided by IConnexityBuffer to ConnexityProvider
    /// </summary>
    public class NeighbourSquares : IEnumerable<Square>
    {
        /// <summary>
        /// The buffer from which to get the squares
        /// </summary>
        private ISquareProvider provider;

        /// <summary>
        /// The coordinates of the square whose neighbours are asked
        /// </summary>
        private Point _position;

        /// <summary>
        /// Initializes a new instance of the NeighbourSquares class
        /// </summary>
        /// <param name="provider">The provider from which to get the squares</param>
        /// <param name="position">The coordinates of the square whose neighbours are asked</param>
        public NeighbourSquares(ISquareProvider provider, Point position)
        {
            this.provider = provider;
            this._position = position;
        }

        /// <summary>
        /// An enumerator used to get access to the "foreach" syntax 
        /// on neighbours of a square.
        /// </summary>
        /// <returns>Yields each a time a new neighbour to the point given at the beginning</returns>
        public IEnumerator<Square> GetEnumerator()
        {
            yield return provider.GetSquareByPosition(_position.GetNorth());
            yield return provider.GetSquareByPosition(_position.GetEast());
            yield return provider.GetSquareByPosition(_position.GetSouth());
            yield return provider.GetSquareByPosition(_position.GetWest());
        }

        /// <summary>
        /// An alias for the other GetEnumerator(), 
        /// to properly implement the IEnumerable interface.
        /// </summary>
        /// <returns>Yields each a time a new neighbour to the point given at the beginning</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            yield return this.GetEnumerator();
        }
    }
}
